﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.IO;
using System.Threading.Tasks;
using System.Threading;

namespace MangaEplision
{
    /// <summary>
    /// Interaction logic for MangaInfoTab.xaml
    /// </summary>
    public partial class MangaInfoTab : TabItem
    {
        private Manga ma = null;
        public MangaInfoTab(Manga mg)
        {
            InitializeComponent();
            ma = mg;
            this.Header = mg.MangaName;
            MangaTitle.Content = mg.MangaName;
            image1.Source = mg.BookImage;
            descbox.AppendText(mg.Description);

            volumelistbox.ItemsSource = mg.Books;

            volumelistbox.SelectedIndex = 0;
        }

        private void ViewBtn_Click(object sender, RoutedEventArgs e)
        {
            //For setting the current row.
            Button b = (Button)sender;
            volumelistbox.SelectedIndex = (int)b.Content - 1;


            BookEntry be = (BookEntry)volumelistbox.SelectedItem;
            Book bk = null;
            LoadingMangaWin lmw = new LoadingMangaWin();
            lmw.ResizeMode = ResizeMode.NoResize;
            lmw.Topmost = true;
            lmw.ShowInTaskbar = false;
            lmw.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            lmw.Show();
            Task t = Task.Factory.StartNew(delegate()
            {
                try
                {
                    bk = new MangaReaderSource(true).GetBook(ma, be);
                    Dispatcher.Invoke(new EmptyDelegate(delegate()
                    {
                        MangaViewer mv = new MangaViewer(bk);
                        mv.Show();
                    }));
                }
                catch (Exception)
                {
                    
                }
                Dispatcher.Invoke(new EmptyDelegate(delegate()
                {
                    lmw.Close();
                }));
            });           
        }
        private delegate void EmptyDelegate();
        private void DownloadBtn_Click(object sender, RoutedEventArgs e)
        {
            //For setting the current row.
            Button b = (Button)sender;
            volumelistbox.SelectedIndex = (int)b.Content - 1;

        }
    }
}
